<?php

namespace app\api\service;

use app\api\model\TrainChild;
use app\api\model\TrainList;
use app\api\model\TrainRecord;

class TrainService
{
    public static function getUserTrain($userInfo, $params)
    {
        $info = TrainList::getTodayInfoByUid($userInfo['user_id']);
        if (!$info) return [200, '无训练计划', []];
        $children = TrainChild::getChildListByTrainId($info['train_id']);
        return [200, '操作成功', [
            'train_id' => $info['train_id'],
            'train_title' => $info['train_title'],
            'train_list' => array_map(function ($item) {
                return [
                    'child_id' => $item['child_id'],
                    'video_title' => $item['video_title'],
                    'train_status' => TrainRecord::getTrainRecord($item['child_id']) ? 1 : 0,
                ];
            }, $children),
        ]];
    }

    public static function getTrainInfo($userInfo, $params)
    {
        $info = TrainList::getInfoByTrainId($params['train_id']);
        if (!$info) return [201, '无训练计划', []];
        $children = TrainChild::getListByTrainId($params['train_id']);
        return [200, '操作成功', [
            'train_id' => $info['train_id'],
            'train_list' => array_map(function ($item) {
                return [
                    'child_id' => $item['child_id'],
                    'video_title' => $item['video_title'],
                    'video_url' => dealImage($item['video_url']),
                    'category_id' => $item['category_id'],
                    'category_ap' => $item['category_func'],
                    'train_status' => TrainRecord::getTrainRecord($item['child_id']) ? 1 : 0
                ];
            }, $children),
        ]];
    }

    public static function getTrainRecord($userInfo, $params)
    {
//        $list = TrainList::getHistoryList($userInfo['user_id'], $params['page_no'], $params['page_size']);
//        $total = TrainList::countHistoryList($userInfo['user_id']);
//        $data = array_map(function ($item) {
//            $children = TrainChild::getListByTrainId($item['train_id']);
//            $totalTimes = TrainChild::countTrainTimes($item['train_id']);
//            $unfinishTimes = TrainChild::countFinishTimes($item['train_id']);
//            return [
//                'train_id' => $item['train_id'],
//                'train_title' => $item['train_title'],
//                'train_times' => $totalTimes,
//                'unfinish_times' => $unfinishTimes,
//                'finish_rate' => (int)bcdiv($totalTimes - $unfinishTimes, $totalTimes, 2) * 100,
//                'train_status' => $item['train_status'],
//                'train_list' => array_map(function ($item) {
//                    return [
//                        'child_id' => $item['child_id'],
//                        'video_title' => $item['video_title'],
//                        'train_status' => $item['train_status'],
//                    ];
//                }, $children)];
//        }, $list);
//        return [200, '操作成功', ['total' => $total, 'list' => $data]];
        $list = TrainRecord::getTrainGroupList($userInfo['user_id']);
        $total = TrainRecord::countTrainGroupList($userInfo['user_id']);
        $data = array_map(function ($item) {
            $trainInfo = TrainList::find($item['train_id']);
            $children = TrainChild::getListByTrainId($item['train_id']);
            $unfinish_times = 0;
            $childrenArr = [];
            foreach ($children as $child) {
                $trainStatus = TrainRecord::getTrainRecord($child['child_id']) ? 1 : 0;
                if (!$trainStatus) $unfinish_times += 1;
                $childrenArr[] = [
                    'child_id' => $child['child_id'],
                    'video_title' => $child['video_title'],
                    'train_status' => $trainStatus,
                ];
            }
            return [
                'train_id' => $trainInfo['train_id'],
                'train_title' => $trainInfo['train_title'],
                'train_times' => count($children),
                'unfinish_times' => $unfinish_times,
                'finish_rate' => (int)(bcdiv(count($children) - $unfinish_times, count($children), 2) * 100),
                'train_status' => (int)(count($children) == $unfinish_times),
                'train_list' => $childrenArr,
            ];
        }, $list);
        return [200, '操作成功', ['total' => $total, 'list' => $data]];
    }

    public static function saveTrainInfo($userInfo, $params)
    {
        $childInfo = TrainChild::getInfoById($params['child_id']);
        if (!$childInfo) return [200, '训练不存在', []];
//        if ($childInfo['train_status']) return [200, '训练已完成', []];
        $result = TrainRecord::createTrainRecord($userInfo['user_id'], $childInfo['train_id'], $childInfo['child_id']);
        if (!$result) return [201, '操作失败', []];
//        $childInfo->save([
//            'train_status' => 1,
//        ]);
//        TrainList::checkTrainFinish($childInfo['train_id']);
        return [200, '操作成功', []];
    }
}